package com.ellecity06.androidaddflutter

import android.app.Application
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.plugin.common.MethodChannel

/**
 * @author ellecity06
 * @time 2022/11/12 12:15
 * @des
 */
class AppApplication :Application()  {
    lateinit var flutterEngine : FlutterEngine
    var count = 0
     lateinit var channel: MethodChannel
    companion object {
        const val ENGINE_ID = "demo_engine_id"
        const val METHOD_CHANNEL = "com.ellecity06/counter"
    }
    override fun onCreate() {
        super.onCreate()
        // Instantiate a FlutterEngine.
        flutterEngine = FlutterEngine(this)

        // Start executing Dart code to pre-warm the FlutterEngine.
        flutterEngine.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint.createDefault()
        )

        // Cache the FlutterEngine to be used by FlutterActivity.
        FlutterEngineCache
            .getInstance()
            .put(ENGINE_ID, flutterEngine)
        channel = MethodChannel(flutterEngine.dartExecutor, METHOD_CHANNEL)

        channel.setMethodCallHandler { call, _ ->
            when (call.method) {
                "incrementCounter" -> {
                    count++
                    reportCounter()
                }
                "requestCounter" -> {
                    reportCounter()
                }
                "setTitle" ->{
//                    channel.invokeMethod("setTitle", "我是传递过啦的标的")
                }
            }
        }
    }
    private fun reportCounter() {
        channel.invokeMethod("reportCounter", count)
    }
}